﻿using System;
using System.Reflection.Emit;
using Sigil.Impl;

namespace Sigil
{
	public partial class Emit<DelegateType>
	{
		/// <summary>
		/// Throws an ArithmeticException on runtime if the value on the stack is not a finite number.
		///
		/// This leaves the value checked on the stack, rather than popping it as might be expected.
		/// </summary>
		public Emit<DelegateType> CheckFinite()
		{
			var transitions =
					new[]
								{
										new StackTransition(new [] { typeof(Single) }, new [] { typeof(Single) }),
										new StackTransition(new [] { typeof(Double) }, new [] { typeof(Double) })
								};

			// ckfinite leaves the value on the stack, oddly enough
			UpdateState(OpCodes.Ckfinite, Wrap(transitions, "CheckFinite"));

			return this;
		}
	}
}